package question.abFile2C;

import java.io.*;
import java.util.Date;
import java.util.Random;

/**
 * 生成测试文件
 * Created by yangliwei on 2018/3/12.
 */
public class GenerateTestFile {
    private static char[] cc = new char[26];
    private static int length = 16;
    private final static String blank= " ";
    private final static String enter= "\n";

    static {
        for (int i = 0; i < 26; i++) {
            cc[i] = (char) ('a' + i);
        }
    }

    public static void main(String[] args) throws IOException {
        String dirPath = GenerateTestFile.class.getResource("").getPath();
        String afileName = "a.txt";
        String bFileName = "b.txt";
        int wordCount = 10000;
        Date startTime = new Date();

        //使用多线程跑
        Thread athread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    fillFile(dirPath+File.separator+afileName,wordCount);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        athread.start();
        Thread bthread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    fillFile(dirPath+File.separator+bFileName,wordCount);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        bthread.start();
        try {
            //join方法阻塞调用的线程，等待被调用线程执行完了才会执行调用线程
            athread.join();
            bthread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        /*
        fillFile(dirPath+File.separator+afileName,wordCount);
        fillFile(dirPath+File.separator+bFileName,wordCount);
        */
        Date endTime = new Date();
        System.out.println(endTime.getTime()-startTime.getTime());
    }

    //用num个单词填充文件
    private static void fillFile(String filePath,int num) throws IOException {
        File file = new File(filePath);
        FileOutputStream out= null;
        Writer writer = null;
        Random rand  = new Random();
        try {
            out = new FileOutputStream(file);
            writer = new OutputStreamWriter(out);
            for(int i = 0;i<num;i++){
                writer.write(generateWord()+(rand.nextBoolean()?blank:enter));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(writer!=null){
                writer.close();
            }
            if(out!=null)
                out.close();
        }

    }

    //生成单词
    private static String generateWord() {
        Random rand = new Random();
        int length = rand.nextInt(GenerateTestFile.length) + 1;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(cc[rand.nextInt(26)]);
        }
        return sb.toString();
    }

    //不使用常量cc生成word
    final private static String generateWord1(){
        Random rand = new Random();
        int length = rand.nextInt(GenerateTestFile.length) + 1;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append((char) ('a'+rand.nextInt(26)));
        }
        return sb.toString();
    }


    /**
     * 开发过程中获取文件路径的方法
     * <p>
     * file:/D:/ideaProject/javaStudy/bin/
     * file:/D:/ideaProject/javaStudy/bin/
     * file:/D:/ideaProject/javaStudy/bin/question/abFile2C/
     * D:\ideaProject\javaStudy
     * D:\ideaProject\javaStudy
     */
    public static void testFilePath() {
        System.out.println(GenerateTestFile.class.getClassLoader().getResource(""));
        System.out.println(ClassLoader.getSystemResource(""));
        System.out.println(GenerateTestFile.class.getResource(""));
        System.out.println(System.getProperty("user.dir"));
        System.out.println(new File("").getAbsoluteFile());
    }
}
